import warnings
warnings.warn("qturboframe.public_ui.ucf_h_ui.menu模块已被废弃，替代模块为qturboframe.public_ui.h_ui.menu", DeprecationWarning)

import re

from qturboframe.lib.report.log import logger
from qturboframe.lib.webdriver.remote.webelement import WebElement
from ..ucf_base_element import BaseElement
from ..binding_menu_options import binding_menu_vue


class Menu(BaseElement):
    '''hui的菜单操作'''

    def select(self, link_text):
        '''
        hui的菜单栏菜单点击

        :Args:
         - link_text: 菜单栏链接名称集合，按照顺序点击链接

        :Raises:
            SeleniumError: 菜单操作失败

        :Example:
         - 例: menu.select('一级菜单/二级菜单/三级菜单')
        '''
        up_ul = self._element.find_element_by_attribute('xpath', './ul', timeout=self.timeout)
        value_list = link_text.split('/')
        for va in value_list:
            if up_ul is not None:
                up_ul = self.__click_node_by_nodename(up_ul, va)

    def __click_node_by_nodename(self, up_ul:WebElement, value):
        ul = None
        re_open_li = r'h-menu-opened'
        re_item_li = r'h-menu-item'
        re_selected_li = r'h-menu-item-selected'
        lis = up_ul.find_elements_by_attribute('xpath', './*/li', timeout=self.timeout)
        for li in lis:
            li_class = li.get_attribute('class')
            if re_item_li == li_class or re_selected_li in li_class:
                if li.find_element_by_attribute('xpath', './span',
                                                timeout=self.timeout).text == value:
                    logger.info('打开菜单：' + value)
                    li.find_element_by_attribute('xpath', './span').secure_click()
                    break
            else:
                if li.find_element_by_attribute('xpath', './div',
                                                timeout=self.timeout).text == value:
                    if not re.search(re_open_li, li_class):
                        logger.info('打开菜单：' + value)
                        li.find_element_by_attribute('xpath:./div/span').secure_click()
                    ul = li.exist('xpath:./ul', timeout=self.timeout)
                    break
        return ul
